CreateConVar( "luahl2weps", 1, { NONE } )
CreateConVar( "notools", 0, { NONE } )

local AmmoLimits = { }

AmmoLimits[ "grenade" ] = 5
AmmoLimits[ "rpg_round" ] = 3
AmmoLimits[ "SMG1_Grenade" ] = 3
AmmoLimits[ "slam" ] = 4
AmmoLimits[ "AR2AltFire" ] = 3
AmmoLimits[ "GaussEnergy" ] = 3
AmmoLimits[ "AlyxGun" ] = 100
AmmoLimits[ "gravity" ] = 1

local PistolLimit = { }

PistolLimit[ "pistol" ] = 120

local MagnumLimit = { }

MagnumLimit[ "357" ] = 18

local SmgLimit = { }

SmgLimit[ "smg1" ] = 225

local Ar2Limit = { }

Ar2Limit[ "ar2" ] = 90

local ShotgunLimit = { }

ShotgunLimit[ "buckshot" ] = 30

local CrossbowLimit = { }

CrossbowLimit[ "XBowBolt" ] = 15

hook.Add( "Tick", "AmmoLimitOther", function( )

	for _, pl in pairs( player.GetAll( ) ) do
	
		for k, v in pairs( AmmoLimits ) do
	
			if ( pl:GetAmmoCount( k ) > v ) then
		
				pl:RemoveAmmo ( pl:GetAmmoCount( k ) - v ,k )
		
			end
	
		end
		
	end

end)

function AmmoLimit1()
if GetConVarNumber("luahl2weps") == 1 then
for _, pl in pairs (player.GetAll( ) ) do
	for k,v in pairs ( PistolLimit ) do
	if !pl:HasWeapon("hl2_pistol") or !pl:GetActiveWeapon():IsValid() then return end
		if ( pl:GetAmmoCount( k ) > (v + pl:GetWeapon("hl2_pistol").Primary.ClipSize - pl:GetWeapon("hl2_pistol"):Clip1()) ) then
		
				pl:RemoveAmmo ( pl:GetAmmoCount( k ) - (v + pl:GetWeapon("hl2_pistol").Primary.ClipSize - pl:GetWeapon("hl2_pistol"):Clip1() ) ,k )
		
		end
end
end
end
end
hook.Add( "Tick", "AmmoLimit1", AmmoLimit1 )

function AmmoLimit2()
if GetConVarNumber("luahl2weps") == 1 then
for _, pl in pairs (player.GetAll( ) ) do
	for k,v in pairs ( MagnumLimit ) do
	if !pl:HasWeapon("hl2_357") or !pl:GetActiveWeapon():IsValid() then return end
		if ( pl:GetAmmoCount( k ) > (v + pl:GetWeapon("hl2_357").Primary.ClipSize - pl:GetWeapon("hl2_357"):Clip1()) ) then
		
				pl:RemoveAmmo ( pl:GetAmmoCount( k ) - (v + pl:GetWeapon("hl2_357").Primary.ClipSize - pl:GetWeapon("hl2_357"):Clip1()) ,k )
		
		end
end
end
end
end
hook.Add( "Tick", "AmmoLimit2", AmmoLimit2 )

function AmmoLimit3()
if GetConVarNumber("luahl2weps") == 1 then
for _, pl in pairs (player.GetAll( ) ) do
	for k,v in pairs ( SmgLimit ) do
	if !pl:HasWeapon("hl2_smg") or !pl:GetActiveWeapon():IsValid() then return end
		if ( pl:GetAmmoCount( k ) > (v + pl:GetWeapon("hl2_smg").Primary.ClipSize - pl:GetWeapon("hl2_smg"):Clip1()) ) then
		
				pl:RemoveAmmo ( pl:GetAmmoCount( k ) - (v + pl:GetWeapon("hl2_smg").Primary.ClipSize - pl:GetWeapon("hl2_smg"):Clip1()) ,k )
		
		end
end
end
end
end
hook.Add( "Tick", "AmmoLimit3", AmmoLimit3 )

function AmmoLimit4()
if GetConVarNumber("luahl2weps") == 1 then
for _, pl in pairs (player.GetAll( ) ) do
	for k,v in pairs ( Ar2Limit ) do
	if !pl:HasWeapon("hl2_ar2") or !pl:GetActiveWeapon():IsValid() then return end
		if ( pl:GetAmmoCount( k ) > (v + pl:GetWeapon("hl2_ar2").Primary.ClipSize - pl:GetWeapon("hl2_ar2"):Clip1()) ) then
		
				pl:RemoveAmmo ( pl:GetAmmoCount( k ) - (v + pl:GetWeapon("hl2_ar2").Primary.ClipSize - pl:GetWeapon("hl2_ar2"):Clip1()) ,k )
		
		end
end
end
end
end
hook.Add( "Tick", "AmmoLimit4", AmmoLimit4 )

function AmmoLimit5()
if GetConVarNumber("luahl2weps") == 1 then
for _, pl in pairs (player.GetAll( ) ) do
	for k,v in pairs ( ShotgunLimit ) do
	if !pl:HasWeapon("hl2_shotgun") or !pl:GetActiveWeapon():IsValid() then return end
		if ( pl:GetAmmoCount( k ) > (v + pl:GetWeapon("hl2_shotgun").Primary.ClipSize - pl:GetWeapon("hl2_shotgun"):Clip1()) ) then
		
				pl:RemoveAmmo ( pl:GetAmmoCount( k ) - (v + pl:GetWeapon("hl2_shotgun").Primary.ClipSize - pl:GetWeapon("hl2_shotgun"):Clip1()) ,k )
		
		end
end
end
end
end
hook.Add( "Tick", "AmmoLimit5", AmmoLimit5 )

function AmmoLimit6()
if GetConVarNumber("luahl2weps") == 1 then
for _, pl in pairs (player.GetAll( ) ) do
	for k,v in pairs ( CrossbowLimit ) do
	if !pl:HasWeapon("hl2_crossbow") or !pl:GetActiveWeapon():IsValid() then return end
		if ( pl:GetAmmoCount( k ) > (v + pl:GetWeapon("hl2_crossbow").Primary.ClipSize - pl:GetWeapon("hl2_crossbow"):Clip1()) ) then
		
				pl:RemoveAmmo ( pl:GetAmmoCount( k ) - (v + pl:GetWeapon("hl2_crossbow").Primary.ClipSize - pl:GetWeapon("hl2_crossbow"):Clip1()) ,k )
		
		end
end
end
end
end
hook.Add( "Tick", "AmmoLimit6", AmmoLimit6 )

function LuaHl2Weapons()
if GetConVarNumber("luahl2weps") == 1 then
	for k, player in pairs(player.GetAll()) do
		if player:HasWeapon("weapon_crowbar") then
		player:Give("hl2_crowbar")
		end
		if player:HasWeapon("weapon_pistol") then
		player:Give("hl2_pistol")
		end
		if player:HasWeapon("weapon_357") then
		player:Give("hl2_357")
		end
		if player:HasWeapon("weapon_smg1") then
		player:Give("hl2_smg")
		end
		if player:HasWeapon("weapon_ar2") then
		player:Give("hl2_ar2")
		end
		if player:HasWeapon("weapon_shotgun") then
		player:Give("hl2_shotgun")
		end
		if player:HasWeapon("weapon_crossbow") then
		player:Give("hl2_crossbow")
		end
		if player:HasWeapon("weapon_frag") then
		player:Give("hl2_grenade")
		end
		local forremove
		if GetConVarNumber("notools") == 1 then
		forremove = {"weapon_crowbar","weapon_smg1","weapon_ar2","weapon_frag","weapon_357","weapon_slam","weapon_pistol","weapon_crossbow","weapon_shotgun","weapon_physgun","gmod_camera","weapon_stunstick","gmod_tool"}
		else
		forremove = {"weapon_crowbar","weapon_smg1","weapon_ar2","weapon_frag","weapon_357","weapon_slam","weapon_pistol","weapon_crossbow","weapon_shotgun","weapon_stunstick"}
		end
		for k, wepname in pairs(forremove) do
			local wepon = player:GetWeapon(wepname)
			if wepon!=NULL then
				player:GiveAmmo(wepon:Clip1(),wepon:GetPrimaryAmmoType());
				player:GiveAmmo(wepon:Clip2(),wepon:GetSecondaryAmmoType());
				player:StripWeapon(wepname)
			end
		end
	end
end
end
hook.Add("Think", "LuaHl2Weapons", LuaHl2Weapons)

function ErrorBoltRemover()
for k,v in pairs (ents.FindInSphere( Vector(0,0,0), 20 )) do
if v:GetClass() == "crossbow_triggerhurt" then v:Remove() end
end
end
hook.Add("Think", "ErrorBoltRemover", ErrorBoltRemover)